/*
 * @Author: szx
 * @Date: 2022-03-10 13:54:11
 * @LastEditTime: 2022-03-10 14:02:20
 * @Description:
 * @FilePath: \leetcode\500-599\589\589.js
 */
/**
 * // Definition for a Node.
 * function Node(val, children) {
 *    this.val = val;
 *    this.children = children;
 * };
 */

/**
 * @param {Node|null} root
 * @return {number[]}
 */
var preorder = function (root) {
    if (!root) return [];
    const res = [];
    const dfs = (root) => {
        res.push(root.val);
        for (let i of root.children) {
            i && dfs(i);
        }
    };
    dfs(root);
    return res;
};

var preorder = function (root) {
    if (!root) return [];
    const st = [root];
    const res = [];
    while (st.length) {
        res.push(st.pop().val);
        for (let i = root.children.length - 1; i >= 0; i--) {
            root.children[i] && st.push(root.children[i]);
        }
    }
    return res;
};
